package com.ruoyi.jim.processor;


import org.jim.core.ImChannelContext;
import org.jim.core.exception.ImException;
import org.jim.core.packets.*;
import org.jim.server.JimServer;
import org.jim.server.JimServerAPI;
import org.jim.server.processor.login.LoginCmdProcessor;
import org.jim.server.protocol.AbstractProtocolCmdProcessor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class LoginServiceProcessor extends AbstractProtocolCmdProcessor implements LoginCmdProcessor {

    private Logger logger = LoggerFactory.getLogger(LoginServiceProcessor.class);

    private static Map<String,User> cacheUser = new HashMap<>();


    @Override
    public LoginRespBody doLogin(LoginReqBody loginReqBody, ImChannelContext imChannelContext) {

        //登入检查
//        String account = loginReqBody.getAccount();
//
//
//        if (StringUtils.isEmpty(account) && StringUtils.isEmpty(loginReqBody.getPassword()))
//            return LoginRespBody.failed("账号或密码不能为空");
//        IMUserService userService = SpringUtils.getBean(IMUserService.class);
//
//        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("account",loginReqBody.getAccount())
//                .eq("password",loginReqBody.getPassword());
//        User user = userService.getOne(queryWrapper);
//
//        if (user != null){
//            user.setStatus(UserStatusType.ONLINE);
//            user.setTerminal(loginReqBody.getDevice());
//            cacheUser.put(account,user);
//            return LoginRespBody.success();
//        }
        return LoginRespBody.failed("账号或密码错误");

    }

    @Override
    public User getUser(LoginReqBody loginReqBody, ImChannelContext imChannelContext) {

        List<ImChannelContext> byUserId = JimServerAPI.getByUserId("");


        return null;
    }


    @Override
    public void onSuccess(User user, ImChannelContext imChannelContext){
        logger.info("登录成功回调方法");

    }

    @Override
    public void onFailed(ImChannelContext imChannelContext) {
        logger.info("登录失败回调方法");
    }

    private String createNum(){

        String str="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        StringBuilder sb = new StringBuilder(4);
        for(int i=0;i<4;i++) {
            char ch=str.charAt(new Random().nextInt(str.length()));
            sb.append(ch);
        }
        return sb.toString();
    }
}
